import axios from 'axios'
import router from '@/router'
import { ElMessage } from 'element-plus'
const instance = axios.create({
    baseURL: "http://localhost:3000/api"
})

instance.interceptors.request.use(config => {
    const { token } = JSON.parse(localStorage.getItem('adminLoginInfo') || '{}')
    if (token) {
        config.headers.Authorization = token
    }
    return config
})

instance.interceptors.response.use(response => {
    if (response.data.code === 403) {
        ElMessage.warning(response.data.msg)
        localStorage.removeItem('adminLoginInfo')
        router.replace('/login')
        return false
    }
    return response
})
export default instance